#pragma once

#include <pthread.h>
#include <iostream>
#include <string>
#include <time.h>


typedef void*(*callback_t)(void*);

class Thread
{
private:
    void Entery()
    {
        _cb(_args);
    }

    static void* Routine(void* args)
    {
        Thread* pt = static_cast<Thread*>(args);
        pt->Entery();
        return nullptr;
    }
public:
    Thread(callback_t cb, void* args) : _isrunning(false), _cb(cb), _args(args)
    {}

    void Run()
    {
        _threadname = "thread-"+std::to_string(num++);
        _start_timestamp = time(nullptr);
        _isrunning = true;
        pthread_create(&_tid, nullptr, Routine, this);
    }

    std::string Name() const
    {
        return _threadname;
    }

    pthread_t Gettid() const
    {
        return _tid;
    }

    void Join()
    {
        pthread_join(_tid, nullptr);
        _isrunning = false;
    }

    uint64_t Starttime()
    {
        return _start_timestamp;
    }

    bool IsRunning()
    {
        return _isrunning;
    }

    ~Thread()
    {}
private:
    pthread_t _tid;
    std::string _threadname;
    time_t _start_timestamp;
    bool _isrunning;

    callback_t _cb;
    void* _args;
    static int num;
};

int Thread::num = 1;